package cn.vcjf.weapp.api.active.controller;


import cn.vcjf.weapp.api.active.entity.GoodsSetmealDetail;
import cn.vcjf.weapp.api.active.service.GoodsSetmealDetailService;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.active.entity.GoodsSetmeal;
import cn.vcjf.weapp.api.active.service.GoodsSetmealService;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 套餐
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-28
 */
@Controller
@RequestMapping("/active/goodsSetmeal")
public class GoodsSetmealController {
	@Resource
	private GoodsSetmealService goodsSetmealService;
	@Resource
	private GoodsSetmealDetailService goodsSetmealDetailService;

	@RequestMapping("/addGoodsSetmeal")
	@ResponseBody
	public TableResult addGoodsSetmeal(GoodsSetmeal goodsSetmeal) {
		goodsSetmeal.preInsert();
		boolean b = goodsSetmealService.insert(goodsSetmeal);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateGoodsSetmeal")
	@ResponseBody
	public TableResult updateGoodsSetmeal(GoodsSetmeal goodsSetmeal) {
		goodsSetmeal.preUpdate();
		boolean b = goodsSetmealService.updateById(goodsSetmeal);
		return TableResult.Result(b);
	}

	@RequestMapping("/findGoodsSetmealOne")
	@ResponseBody
	public TableResult findGoodsSetmealOne(GoodsSetmeal goodsSetmeal) {

		EntityWrapper<GoodsSetmeal> wrapper = goodsSetmeal.getQueryWrapper();

		GoodsSetmeal result = goodsSetmealService.selectOne(wrapper);

		Wrapper<GoodsSetmealDetail> query = new GoodsSetmealDetail().getQueryWrapper();
		query.eq("gsd.setmealId",result.getId());
		List<GoodsSetmealDetail> goodsSetmealDetails = goodsSetmealDetailService.selectList(query);
		result.setGoodsSetmealDetailList(goodsSetmealDetails);

		return TableResult.OKResult(result);
	}

	/**
	 * 精选套餐
	 * @param goodsSetmeal
	 * @return
	 */
	@RequestMapping("/findGoodsSetmealList")
	@ResponseBody
	public TableResult findGoodsSetmealList(GoodsSetmeal goodsSetmeal) {

		EntityWrapper<GoodsSetmeal> wrapper = goodsSetmeal.getQueryWrapper();
		//Todo
		wrapper.orderBy("sortNo",false);

		Page<GoodsSetmeal> page = goodsSetmeal.getPageInfo();

		page = goodsSetmealService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

	@RequestMapping("/deleteGoodsSetmeal")
	@ResponseBody
	public TableResult deleteGoodsSetmeal(GoodsSetmeal goodsSetmeal) {
		goodsSetmeal.preUpdate();
		boolean b = goodsSetmealService.deleteById(goodsSetmeal.getId());
		return TableResult.Result(b);
	}

}
